<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="缴费名" prop="totalTitle">
        <el-input
          v-model="queryParams.totalTitle"
          placeholder="请输入缴费名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择">
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.nickName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="总金额" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始有效日期" prop="startTime">
        <el-date-picker
          clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始有效日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束有效期" prop="endTime">
        <el-date-picker
          clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束有效期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="缴费状态" prop="orderStatus">
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="请选择缴费状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.pay_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:payment:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:payment:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:payment:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:payment:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="paymentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column
        label="用户名"
        align="center"
        prop="userName"
        width="150"
      >
        <template slot-scope="scope">
          <el-tag>{{ scope.row.userName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="查看缴费项" align="center">
        <template slot-scope="scope">
          <el-popover placement="right" width="400" trigger="click">
        <el-table :data="scope.row.paymentItemList">
          <el-table-column
            width="300"
            property="title"
            label="缴费名"
          ></el-table-column>
          <el-table-column
            width="300"
            property="amount"
            label="缴费金额"
          ></el-table-column>
        </el-table>
        <el-button slot="reference">查看缴费项</el-button>
      </el-popover>
        </template>
        
      </el-table-column>
     
      <el-table-column label="缴费名" align="center" prop="totalTitle" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column
        label="开始有效日期"
        align="center"
        prop="startTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束有效期"
        align="center"
        prop="endTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缴费状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.pay_status"
            :value="scope.row.orderStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:payment:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:payment:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改缴费管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="缴费名" prop="totalTitle">
          <el-input v-model="form.totalTitle" placeholder="请输入缴费名" />
        </el-form-item>
        <el-form-item label="用户" prop="userId">
          <el-select
            v-model="form.payUserId"
            @change="changeUser"
            placeholder="请选择"
          >
            <el-option
              v-for="item in userList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绑定房屋" prop="buildId">
          <el-select
            v-model="form.buildId"
            @change="changeBuild"
            placeholder="请选择"
          >
            <el-option
              v-for="item in buildList"
              :key="item.buildId"
              :label="item.houseName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmount">
          <el-input
            v-model="form.totalAmount"
            placeholder="请输入总金额"
            disabled
          />
        </el-form-item>
        <el-form-item label="开始有效日期" prop="startTime">
          <el-date-picker
            clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始有效日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束有效期" prop="endTime">
          <el-date-picker
            clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束有效期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="缴费状态" prop="orderStatus">
          <el-select v-model="form.orderStatus" placeholder="请选择缴费状态">
            <el-option
              v-for="dict in dict.type.pay_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">缴费项信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddPaymentItem"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeletePaymentItem"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="paymentItemList"
          :row-class-name="rowPaymentItemIndex"
          @selection-change="handlePaymentItemSelectionChange"
          ref="paymentItem"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="缴费项目名" prop="title" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.title"
                placeholder="请输入缴费项目名"
              />
            </template>
          </el-table-column>
          <el-table-column label="缴费金额" prop="amount" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.amount"
                placeholder="请输入缴费金额"
              />
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="sort" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sort" placeholder="请输入排序" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPayment,
  getPayment,
  delPayment,
  addPayment,
  updatePayment,
  getUserList,
  getBuildList
} from "@/api/base/payment";

export default {
  name: "Payment",
  dicts: ["pay_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPaymentItem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 缴费管理表格数据
      paymentList: [],
      // 缴费项表格数据
      paymentItemList: [],
      // 弹出层标题
      title: "",
      userList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        totalTitle: null,
        totalAmount: null,
        startTime: null,
        endTime: null,
        orderStatus: null,
        sort: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      buildList:[]
    };
  },
  created() {
    this.getList();
    this.getUserList();
  },
  methods: {
    /** 查询缴费管理列表 */
    getList() {
      this.loading = true;
      listPayment(this.queryParams).then((response) => {
        this.paymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    changeUser(e) {
      console.log(e);
      this.getBuildList(e);
      this.form.userId = e;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        updateTime: null,
        updateBy: null,
        createBy: null,
        createTime: null,
        totalTitle: null,
        totalAmount: null,
        startTime: null,
        endTime: null,
        orderStatus: null,
        sort: null,
        remark: null,
      };
      this.paymentItemList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getBuildList(userId){
      getBuildList(userId).then(res=>{
        this.buildList = res.rows;
      })
    },
    getUserList() {
      getUserList().then((res) => {
        this.userList = res.rows;
      });
    },
    changeBuild(e){
      if(form.userId){
       this.form.buildId=e;
      }else{
        this.$modal.msgError("请先选择用户！！");
        return;
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加缴费管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getBuildList(row.userId);
      const id = row.id || this.ids;
      getPayment(id).then((response) => {
        this.form = response.data;
        this.paymentItemList = response.data.paymentItemList;
        this.open = true;
        this.title = "修改缴费管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.paymentItemList = this.paymentItemList;
          if (this.form.id != null) {
            updatePayment(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayment(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除缴费管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delPayment(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 缴费项序号 */
    rowPaymentItemIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 缴费项添加按钮操作 */
    handleAddPaymentItem() {
      let obj = {};
      obj.title = "";
      obj.amount = "";
      obj.sort = "";
      obj.remark = "";
      this.paymentItemList.push(obj);
    },
    /** 缴费项删除按钮操作 */
    handleDeletePaymentItem() {
      if (this.checkedPaymentItem.length == 0) {
        this.$modal.msgError("请先选择要删除的缴费项数据");
      } else {
        const paymentItemList = this.paymentItemList;
        const checkedPaymentItem = this.checkedPaymentItem;
        this.paymentItemList = paymentItemList.filter(function (item) {
          return checkedPaymentItem.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handlePaymentItemSelectionChange(selection) {
      this.checkedPaymentItem = selection.map((item) => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "base/payment/export",
        {
          ...this.queryParams,
        },
        `payment_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
